package package1;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Set;

public class TestForProperties {
/**
 * properties allows data to be permanent on disk
 * @param args
 * @throws IOException
 */
	public static void main(String[] args) throws IOException {
		propertiesTest();
		propertiesLoadTest();
		propertiesLogFolderNameAndAddr();
		filterTest();
	}

	private static void filterTest() {
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				File[]files = dir.listFiles();
				boolean fileFound = false;
				for(File file:files)if(file.getName().equalsIgnoreCase(name))fileFound = true;
				System.err.println("hello!");
				if(!fileFound)
					return false;
				return name.endsWith(".txt");
			}
		};
		String fileAdd = "C:/Users/Alvin/Desktop";
		File f = new File(fileAdd);
		boolean bol = filter.accept(f, ".txt");
		for(File file:f.listFiles())
		{
			System.err.println(file.getName());
		}
		System.err.println("bol:"+bol);
	}

	private static void propertiesLogFolderNameAndAddr() throws IOException {
		String fileAdd = "D:/Program Files/Java/WorkSpace/JavaLearning/src";
		File f = new File(fileAdd);
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.endsWith(".java");
			}
		};
		ArrayList<File>list = new ArrayList<File>();
		getFiles(f,filter,list);
		for(File ff:list){
			System.err.println(ff.getName()+"\t"+ff.getAbsolutePath());
		}
		String fileDes = "javaFileLog.txt";
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(fileDes));
			for(File ff:list){
				bw.write(ff.getAbsolutePath());
				bw.newLine();
				bw.flush();
			}
		} catch (IOException e) {
			System.err.println("writer failed");
			e.printStackTrace();
		}finally{
			if(bw!=null)
				try {
					bw.close();
				} catch (IOException e) {
					System.err.println("close failed");
					e.printStackTrace();
				}
		}
		System.exit(0);
	}

	private static void getFiles(File f, FilenameFilter filter,	ArrayList<File> list) {
		File[] files = f.listFiles();
		for(File file:files)
		{
			if(file.isDirectory())
			{
				getFiles(file, filter, list);
			}
			else
			{
				if(filter.accept(file, file.getName()))
					list.add(file);
			}
		}
			
	}

	private static void propertiesLoadTest() throws IOException {
		File f = new File("log.ini");
		if(!f.exists())
			f.createNewFile();
		Properties prop = new Properties();
		FileInputStream fis = new FileInputStream(f);
		prop.load(fis);
		int count = 0;
		String countStr = prop.getProperty("time");
		if(countStr!=null)
		{
			count = Integer.parseInt(countStr);
			if(count>=5)
			{
				throw new RuntimeException("more than 5 times of use, please buy");
			}
		}
		count++;
		prop.setProperty("time", count+"");
		prop.list(System.out);
		
		FileOutputStream fos = new FileOutputStream(f);
		prop.store(fos, null);
		fis.close();
		fos.close();
		
	}

	private static void propertiesTest() throws IOException {
		Properties p = new Properties();
//		p = System.getProperties();
		p.setProperty("abc", "123");
		Set<Object> set = p.keySet();
		for(Object obj:set){
//			System.err.println(obj);
//			System.err.println(p.get(obj));
		}
		OutputStream os = new FileOutputStream(new File("properties test.txt"));
		p.store(os, "");
		p.list(System.out);
//		os.close();
		p.load(new FileInputStream(new File("properties test.txt")));
		p.list(System.out);
		
	}

}
